﻿using VideoServices.Domain.Entities;
using System.Linq;
using System.Collections.Generic;

namespace VideoServices.Domain.DAL
{
    public interface IStockDataAccess
    {
        /// <summary>
        /// Gets the holding quantity.
        /// </summary>
        /// <param name="catalogueId">The catalogue id.</param>
        /// <param name="warehouseId">The warehouse id.</param>
        /// <returns></returns>
        int GetHoldingQuantity(int catalogueId, int warehouseId);

        /// <summary>
        /// Gets a stock item by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        Stock GetItemById(int id);

        /// <summary>
        /// Gets a stock item by warehouse.
        /// </summary>
        /// <param name="warehouseName">Name of the warehouse.</param>
        /// <returns></returns>
        IEnumerable<Stock> GetItemsByWarehouse(string warehouseName);

        /// <summary>
        /// Creates a stock item.
        /// </summary>
        /// <param name="transientStock">The transient stock.</param>
        /// <returns></returns>
        Stock CreateItem(Stock transientStock);

        /// <summary>
        /// Increments the stock.
        /// </summary>
        /// <param name="catalogue">The catalogue.</param>
        /// <param name="warehouse">The warehouse.</param>
        /// <param name="quantity">The quantity.</param>
        void IncrementStock(Catalogue catalogue, Warehouse warehouse, int quantity);

        /// <summary>
        /// Decrements the stock.
        /// </summary>
        /// <param name="catalogue">The catalogue.</param>
        /// <param name="warehouse">The warehouse.</param>
        /// <param name="quantity">The quantity.</param>
        void DecrementStock(Catalogue catalogue, Warehouse warehouse, int quantity);
    }

    public class StockDataAccess : BaseDataAccess, IStockDataAccess
    {
        #region Implementation of IStockDataAccess

        /// <summary>
        /// Creates the item.
        /// </summary>
        /// <param name="newStock">The new stock.</param>
        /// <returns></returns>
        public Stock CreateItem(Stock newStock)
        {
            DataContext.Stocks.InsertOnSubmit(newStock);
            DataContext.SubmitChanges();
            return newStock;
        }

        /// <summary>
        /// Gets a stock item by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Stock GetItemById(int id)
        {
            return DataContext.Stocks.SingleOrDefault(s => s.id == id);
        }

        /// <summary>
        /// Gets a stock item by warehouse.
        /// </summary>
        /// <param name="warehouseName">Name of the warehouse.</param>
        /// <returns></returns>
        public IEnumerable<Stock> GetItemsByWarehouse(string warehouseName)
        {
            // Here LINQ to SQL automatically generates a INNER JOIN clause with the warehouse table
            var items = DataContext.Stocks.Where(s => s.Warehouse.name == warehouseName).AsEnumerable();
            return items;
        }

        /// <summary>
        /// Gets the holding quantity.
        /// </summary>
        /// <param name="catalogueId">The catalogue id.</param>
        /// <param name="warehouseId">The warehouse id.</param>
        /// <returns></returns>
        public int GetHoldingQuantity(int catalogueId, int warehouseId)
        {
            return DataContext.Stocks.Where(s => s.catalogue_id == catalogueId && s.warehouse_id == warehouseId)
                                     .Select(s => s.holding).Single();
        }

        /// <summary>
        /// Increments the stock.
        /// </summary>
        /// <param name="catalogue">The catalogue.</param>
        /// <param name="warehouse">The warehouse.</param>
        /// <param name="quantity">The quantity.</param>
        public void IncrementStock(Catalogue catalogue, Warehouse warehouse, int quantity)
        {
            //TODO: Implement this method
        }

        /// <summary>
        /// Decrements the stock.
        /// </summary>
        /// <param name="catalogue">The catalogue.</param>
        /// <param name="warehouse">The warehouse.</param>
        /// <param name="quantity">The quantity.</param>
        public void DecrementStock(Catalogue catalogue, Warehouse warehouse, int quantity)
        {
            //TODO: Implement this method
        }

        #endregion
    }
}
